<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="出入库时间" label-width="150">
        <el-date-picker
          v-model="daterangeBusiDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型, 0 入库, 1 出库" clearable>
          <el-option
            v-for="dict in dict.type.sys_store_churu"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择0 正常, -1 作废" clearable>
          <el-option
            v-for="dict in dict.type.sys_churu_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="仓库" prop="storeId">
        <el-select v-model="queryParams.storeId" placeholder="请选择仓库" clearable>
          <el-option
            v-for="dict in storeList"
            :key="dict.id"
            :label="dict.storeName"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:bill:add']"
        >入库</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handleOut"
          v-hasPermi="['system:bill:edit']"
        >出库</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:bill:remove']"
        >批量作废</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:bill:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="billList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="单据流水号" align="center" prop="id" />
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_store_churu" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="仓库" align="center" prop="storeName" />
      <el-table-column label="总数量" align="center" prop="totalCount" />
      <el-table-column label="总金额" align="center" prop="totalAmount" />
      <el-table-column label="出入库时间" align="center" prop="busiDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.busiDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_churu_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作时间" align="center" prop="operateDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.operateDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作人" align="center" prop="operatorName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            v-if="scope.row.status === 0"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:bill:remove']"
          >作废</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:bill:edit']"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog
      title="详细"
      :visible.sync="dialogVisible"
      width="70%"
      :before-close="handleClose">
      <div class="app-container">
        <!-- 添加或修改出入库单据对话框 -->
        <el-form :model="form" label-width="80px">
          <el-form-item label="仓库" prop="type">
            <el-select v-model="form.storeId" placeholder="请选择仓库" disabled>
              <el-option
                v-for="dict in storeList"
                :key="dict.id"
                :label="dict.storeName"
                :value="dict.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="入库时间" prop="busiDate">
            <el-date-picker clearable
                            readonly
                            v-model="form.busiDate"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择入库时间">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="备注" prop="remark" >
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" readonly/>
          </el-form-item>
          <el-form-item prop="item">
            <el-table
              :data="form.item"
              style="width: 100%">
              <el-table-column
                label="序号"
                type="index" width="100">
              </el-table-column>
              <el-table-column
                label="物品"
                prop="goodsName">
              </el-table-column>
              <el-table-column
                label="价格(单位：元)"
                prop="price" align="center">
                <template v-slot="scope">
                  <el-input v-model="scope.row.price" style="width: 80px" readonly></el-input>
                </template>
              </el-table-column>
              <el-table-column
                label="数量"
                prop="amounts">
                <template v-slot="scope">
                  <el-input v-model="scope.row.amounts" style="width: 80px" readonly></el-input>
                </template>
              </el-table-column>
              <el-table-column
                label="小计(单位：元)"
                prop="total">
                <template v-slot="scope">
                  <span style="width: 100px">{{ scope.row.amounts * scope.row.price }}</span>
                </template>
              </el-table-column>
            </el-table>
          </el-form-item>
        </el-form>
        <el-button type="primary" @click="dialogVisible = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listBill,queryBill, delBill} from "@/api/goods/bill";
import {listStore} from "@/api/goods/store";

export default {
  name: "Bill",
  dicts: ['sys_churu_status', 'sys_store_churu'],
  data() {
    return {
      storeList:[],
      dialogVisible:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 出入库单据表格数据
      billList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 备注时间范围
      daterangeBusiDate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        storeId: null,
        busiDate: null,
        status: null,
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
    listStore().then(res=>{
      console.log(res)
      this.storeList = res.rows
    })
  },
  methods: {
    /** 查询出入库单据列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeBusiDate && '' != this.daterangeBusiDate) {
        this.queryParams.params["beginBusiDate"] = this.daterangeBusiDate[0];
        this.queryParams.params["endBusiDate"] = this.daterangeBusiDate[1];
      }
      listBill(this.queryParams).then(response => {
        this.billList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleClose(){
      this.dialogVisible = false
    },
    handleOut(){
      this.$router.push({path:`/bill/outgoing`})
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        storeId: null,
        busiDate: null,
        status: null,
        operateDate: null,
        operatorId: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeBusiDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({path:`/bill/storage`})
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.form = {...row}
      queryBill(row.id).then(res=>{
        console.log(res)
        this.form.item = res.data
        console.log(this.form.item)
        this.dialogVisible = true
      })

    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认将出入库单据编号为"' + ids + '"的数据项作废？').then(function() {
        return delBill(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/bill/export', {
        ...this.queryParams
      }, `bill_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
